package kr.or.smardi.smardi_AScan;

import java.util.*;

import kr.or.smardi.smardi_AScan.Help.*;
import kr.or.smardi.smardi_AScan.Scan.*;
import kr.or.smardi.smardi_AScan.db.*;
import android.app.*;
import android.content.*;
import android.os.*;
import android.view.*;
import android.widget.*;

public class AC_Loading extends Activity {
    
	ImageView loadingWrap = null;
	
	private final long COUNT_END_TIME = 4000;
	private final long COUNT_INTERVAL_TIME = 160;
	
	ArrayList<Integer> loadingList = null;
	
	//SharedPreference
	Manage_SharedPreference mSharedPreference;
	
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_loading);
        
        loadAnimationImages();
        loadingWrap = (ImageView)findViewById(R.id.loadingWrap);
        
        //SharedPreference
		mSharedPreference = new Manage_SharedPreference(AC_Loading.this);
		
        //타이머 시작
        cTimer.start();
        
        RelativeLayout layout_loading = (RelativeLayout)findViewById(R.id.layout_loading);
        
        if(getString(R.string.language).equals("en") == true) {
        	((ImageView)findViewById(R.id.img_warning)).setImageDrawable(getResources().getDrawable(R.drawable.warning_title_en));
        }
        
        layout_loading.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//화면이 클릭되면 scan_main 액티비티로 이동
				movetoNextActivity();
			}
		});
    }
	
	private void loadAnimationImages() {
		loadingList = new ArrayList<Integer>();
		
		loadingList.add(R.drawable.background_loading_1);
		loadingList.add(R.drawable.background_loading_2);
		loadingList.add(R.drawable.background_loading_3);
		loadingList.add(R.drawable.background_loading_4);
		loadingList.add(R.drawable.background_loading_5);
		loadingList.add(R.drawable.background_loading_6);
		loadingList.add(R.drawable.background_loading_7);
		loadingList.add(R.drawable.background_loading_8);
		loadingList.add(R.drawable.background_loading_9);
		loadingList.add(R.drawable.background_loading_10);
		loadingList.add(R.drawable.background_loading_11);
		loadingList.add(R.drawable.background_loading_12);
		loadingList.add(R.drawable.background_loading_13);
		loadingList.add(R.drawable.background_loading_14);
		loadingList.add(R.drawable.background_loading_15);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		
		cTimer.cancel();
	}



	CountDownTimer cTimer = new CountDownTimer(COUNT_END_TIME, COUNT_INTERVAL_TIME) {
		int count = 0;
		
		@Override
		public void onTick(long millisUntilFinished) {
			count++;
			if(count < loadingList.size()) {
				loadingWrap.setImageDrawable(getResources().getDrawable(loadingList.get(count)));
			}
		}
		
		@Override
		public void onFinish() {
			//move to scan_main Activity 
			movetoNextActivity();
		}
	};
	
	
	private void movetoNextActivity() {
		Intent intent;
		
		String userName = mSharedPreference.getUserName();
		
		
		if(userName.equals("") == true || mSharedPreference.getCheckedTutorial() == false) {
			//사용자 등록이 안 되어 있는 경우
			//intent = new Intent(AC_Loading.this, AC_UserAdd.class);
			intent = new Intent(AC_Loading.this, AC_Help_tutorial.class);
		} else {
			//사용자 등록이 되있는 경우
			intent = new Intent(AC_Loading.this, AC_Scan_main.class);
			//intent = new Intent(AC_Loading.this, AC_Help_tutorial.class);
		}
		
		
		startActivity(intent);
		finish();
	}
	
}